<!--
 厦门大学计算机专业 | 前华为工程师
 专注《零基础学编程系列》https://cxyxy.blog.csdn.net/article/details/121134634
 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 公众号：蓝不蓝编程
-->
<template>
  <div class="container">
    <div class="display-container">
      <span class="red_ball">{{ ball1 }}</span>
      <span class="red_ball">{{ ball2 }}</span>
      <span class="red_ball">{{ ball3 }}</span>
      <span class="red_ball">{{ ball4 }}</span>
      <span class="red_ball">{{ ball5 }}</span>
      <span class="red_ball">{{ ball6 }}</span>
      <span class="blue_ball">{{ ball7 }}</span>
    </div>

    <div class="button-container">
      <div class="button-start-circle">
        <div class="button-start" @click="startOrPauseTimer">
          <span class="button-text">{{ startPauseButtonText }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HspPage',
  data() {
    return {
      startPauseButtonText: '开始',
      isStarted: false,
      interval: 0,
      ball1: 15,
      ball2: 15,
      ball3: 15,
      ball4: 15,
      ball5: 15,
      ball6: 15,
      ball7: 15
    }
  },
  methods: {
    startOrPauseTimer() {
      if (this.isStarted) {
        if (this.interval !== 0) {
          clearInterval(this.interval)
          this.interval = 0
        }
        this.startPauseButtonText = '开始'
      } else {
        this.startPauseButtonText = '暂停'

        if (this.interval === 0) {
          this.interval = setInterval(() => {
            this.ball1 = this.formatNumber(this.getRandom(33))
            this.ball2 = this.formatNumber(this.getRandom(33))
            this.ball3 = this.formatNumber(this.getRandom(33))
            this.ball4 = this.formatNumber(this.getRandom(33))
            this.ball5 = this.formatNumber(this.getRandom(33))
            this.ball6 = this.formatNumber(this.getRandom(33))
            this.ball7 = this.formatNumber(this.getRandom(16))
          }, 100)
        }
      }

      this.isStarted = !this.isStarted
    },
    resetTimer() {
      if (this.interval !== 0) {
        clearInterval(this.interval)
        this.interval = 0
      }
      this.isStarted = false
      this.startPauseButtonText = '开始'
    },
    getRandom(maxNum) {
      return Math.ceil(Math.random() * maxNum)
    },
    formatNumber(n) {
      n = n.toString()
      return n[1] ? n : `0${n}`
    }
  }
}
</script>

<style scoped>
.display-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin-top: 10rem;
}
.red_ball {
  color: white;
  font-size: 1.5em;
  background-color: #ce0000;
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  border: 2px solid black;
  display: flex;
  align-items: center;
  justify-content: center;
}
.blue_ball {
  color: white;
  font-size: 1.5em;
  background-color: #489cf5;
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  border: 2px solid black;
  display: flex;
  align-items: center;
  justify-content: center;
}

.button-container {
  display: flex;
  flex-direction: row;
  flex: 1;
  justify-content: space-around;
  width: 100%;
  align-items: space-around;
  padding-top: 15rem;
}

.button-start-circle {
  background-color: #489cf5;
  height: 8.5rem;
  width: 8.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.button-start {
  color: white;
  background-color: #489cf5;
  height: 8rem;
  width: 8rem;
  border-radius: 50%;
  border: 2px solid black;
  display: flex;
  align-items: center;
  justify-content: center;
}

.button-text {
  font-size: 1.5em;
  font-weight: 400;
}
</style>
